Practice Problems on Pointers in C
Posted on December 18, 2023 by Vishesh Namdev
Python
C
C++
Java
Basic Practice Problems based on chapter which you learn in previous Tutorials.
Access Array Elements using Pointer
#include <iostream>
int main() {
// Declare an array
int numbers[] = {1, 2, 3, 4, 5};
// Declare a pointer and initialize it with the address of the first element of the array
int *ptr = numbers;
// Access and display array elements using the pointer
std::cout << "Array elements using pointer: ";
for (int i = 0; i < 5; ++i) {
std::cout << *(ptr + i) << " ";
}
std::cout << std::endl;
return 0;
}
int main() {
// Declare an array
int numbers[] = {1, 2, 3, 4, 5};
// Declare a pointer and initialize it with the address of the first element of the array
int *ptr = numbers;
// Access and display array elements using the pointer
std::cout << "Array elements using pointer: ";
for (int i = 0; i < 5; ++i) {
std::cout << *(ptr + i) << " ";
}
std::cout << std::endl;
return 0;
}